﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormLoaiHang : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private int idtam = 0;
        public FormLoaiHang()
        {
            InitializeComponent();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormLoaiHangThem();
            f.ShowDialog();
        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {
            var f = new FormLoaiHangCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListLoaiHang();
            KiemTra();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormLoaiHang_Load(object sender, EventArgs e)
        {
            GetListLoaiHang();
            KiemTra();
        }
        private void dgvLoaiHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvLoaiHang.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }
        private void dgvLoaiHang_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvLoaiHang.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }
        private void dgvLoaiHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvLoaiHang.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
            var f = new FormLoaiHangCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }
        private void dgvLoaiHang_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvLoaiHang.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
            var f = new FormLoaiHangCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }
        void GetListLoaiHang()
        {
            var ds = db.LoaiHangs;
            dgvLoaiHang.DataSource = ds.ToList();
            idtam = 0;
        }

        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }

        void Delete()
        {
            try
            {
                var t = db.LoaiHangs.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa nhóm: " + t.TenLoaiHang + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    db.LoaiHangs.Remove(t);
                    db.SaveChanges();
                    GetListLoaiHang();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }
    }
}
